﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.PutOutCard;

namespace CardLogistics.DAL.PutOutCard
{
    /// <summary>
    /// 半成品/成品卡需求单_数据访问类
    /// </summary>
    public class PutOutDemandData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_PutOutDemand", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }


    /// <summary>
    /// 判断卡片需求数量是否大于采购需求数量
    /// </summary>
    /// <param name="startDate">需求开始时间</param>
    /// <param name="endDate">需求截止时间</param>
    /// <returns></returns>
        public string strWhere(string startDate, string endDate)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from purchase_demand P");
            strSql.Append(" left join purchase_detailed I on I.DEMANDID =P.ID");
            strSql.Append(" where '" + startDate + "'>=P.DEMANDSTARTDATE and '" + endDate + "'<=P.Demandenddate");
            strSql.Append(" and P.ISDELETE=0 and P.ISSUBMIT=5");
            return strSql.ToString();
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PutOutDemand GetModel(string rId)
        {
            PutOutDemand model = new PutOutDemand();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["DEMANDNO"] != null)
                {
                    model.DEMANDNO = row["DEMANDNO"].ToString();
                }
                if (row["DEMANDSTARTDATE"] != null && row["DEMANDSTARTDATE"].ToString() != "")
                {
                    model.DEMANDSTARTDATE = DateTime.Parse(row["DEMANDSTARTDATE"].ToString());
                }
                if (row["DEMANDENDDATE"] != null && row["DEMANDENDDATE"].ToString() != "")
                {
                    model.DEMANDENDDATE = DateTime.Parse(row["DEMANDENDDATE"].ToString());
                }
                if (row["REASON"] != null)
                {
                    model.REASON = row["REASON"].ToString();
                }
                if (row["REMARK"] != null)
                {
                    model.REMARK = row["REMARK"].ToString();
                }
                if (row["ISSUBMIT"] != null && row["ISSUBMIT"].ToString() != "")
                {
                    model.ISSUBMIT = decimal.Parse(row["ISSUBMIT"].ToString());
                }
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(PutOutDemand model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
				  	new OracleParameter("vDEMANDNO", OracleType.NVarChar),
					new OracleParameter("vDEMANDSTARTDATE", OracleType.DateTime),
					new OracleParameter("vDEMANDENDDATE", OracleType.DateTime),
					new OracleParameter("vREASON", OracleType.NVarChar),
					new OracleParameter("vREMARK", OracleType.NVarChar),
					new OracleParameter("vISSUBMIT", OracleType.Number,4),
                    new OracleParameter("vDetails", OracleType.VarChar,2000)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.DEMANDNO;
                parameters[2].Value = model.DEMANDSTARTDATE;
                parameters[3].Value = model.DEMANDENDDATE;
                parameters[4].Value = model.REASON;
                parameters[5].Value = model.REMARK;
                parameters[6].Value = model.ISSUBMIT;
                parameters[7].Value = model.Details;

                string StrOutPar = DbHelperSQL.RunProcedure("P_PutOutDemand_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(PutOutDemand model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("vID", OracleType.VarChar,50),
				    new OracleParameter("vDEMANDNO", OracleType.NVarChar),
					new OracleParameter("vDEMANDSTARTDATE", OracleType.DateTime),
					new OracleParameter("vDEMANDENDDATE", OracleType.DateTime),
					new OracleParameter("vREASON", OracleType.NVarChar),
					new OracleParameter("vREMARK", OracleType.NVarChar),
					new OracleParameter("vISSUBMIT", OracleType.Number,4),
                    new OracleParameter("vDetails", OracleType.VarChar,2000),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.DEMANDNO;
                parameters[2].Value = model.DEMANDSTARTDATE;
                parameters[3].Value = model.DEMANDENDDATE;
                parameters[4].Value = model.REASON;
                parameters[5].Value = model.REMARK;
                parameters[6].Value = model.ISSUBMIT;
                parameters[7].Value = model.Details;
                parameters[8].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_PutOutDemand_Update", parameters, parameters[8]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_PutOutDemand_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
